import type { PhaseType } from "@/types/micro-scene";
import { api } from "../api";
import Loading from "@/components/Loading";

// 获取单个微场景(详情页最上方卡片)
export const getSceneOverviewDetail = async (microSceneId: string) => {
  Loading.show('加载中...')
  try {
    // path 参数
    const response = await api.get(`/java/ground/aix/scene/get_scene/${microSceneId}`);
    return response.data;
  }
  catch (error) {
    console.error("获取微场景详情失败", error);
    throw new Error("获取微场景详情失败");
  }
  finally {
    Loading.hide()
  }
};

// 更改单个微场景(详情页最上方卡片)
export const updateSceneOverviewDetail = async (sceneId: string, sceneTitle: string, introduction: string, userList: string[]) => {
  Loading.show('更改中...')
  try {
    // query 参数
    const response = await api.post('/java/ground/aix/scene/update_scene', {
      sceneId,
      sceneTitle,
      introduction,
      userList,
    });
    return response.data;
  }
  catch (error) {
    console.error("更改微场景详情失败", error);
    throw new Error("更改微场景详情失败");
  }
  finally {
    Loading.hide()
  }
};

// 搜索用户
export const searchUser = async (query: string) => {
  try {
    const response = await api.get(`/java/user/search_user_by_id_and_name`, {
      params: {
        query,
      },
    });
    return response.data;
  }
  catch (error) {
    console.error("搜索用户失败", error);
    throw new Error("搜索用户失败");
  }
};

// 更改微场景的发布状态
export const updateMicroScenePublishStatus = async (microSceneId: string) => {
  try {
    // path 参数
    const response = await api.post(`/java/ground/aix/scene/toggle_publish/${microSceneId}`);
    return response.data;
  }
  catch (error) {
    console.error("更改微场景的发布状态失败", error);
    throw new Error("更改微场景的发布状态失败");
  }
}

// 获取微场景发布状态
export const getMicroScenePublishStatus = async (microSceneId: string) => {
  try {
    // path 参数
    const response = await api.post(`/java/ground/aix/scene/get_publish/${microSceneId}`);
    return response.data;
  }
  catch (error) {
    console.error("获取微场景的发布状态失败", error);
    throw new Error("获取微场景的发布状态失败");
  }
}

// 获取微场景阶段列表
export const getScenePhaseList = async (microSceneId: string) => {
  try {
    // path 参数
    const response = await api.get(`/java/ground/aix/phase/get_phase_list/${microSceneId}`);
    return response.data;
  }
  catch (error) {
    console.error("获取微场景阶段列表失败", error);
    throw new Error("获取微场景阶段列表失败");
  }
};

// 更新微场景阶段列表
export const updateScenePhaseList = async (microSceneId: string, phaseList: PhaseType[]) => {
  try {
    const response = await api.post(`/java/ground/aix/phase/update_phase_list`, {
      sceneId: microSceneId,
      phaseDTOList: phaseList,
    });
    return response.data;
  }
  catch (error) {
    console.error("更新微场景阶段列表失败", error);
    throw new Error("更新微场景阶段列表失败");
  }
}

// 获取单个阶段的信息
export const getPhaseInfo = async (sceneId: string, phaseId: string) => {
  try {
    // query 参数
    const response = await api.get('/java/ground/aix/phase/get_phase', {
      params: {
        sceneId,
        phaseId,
      },
    });
    return response.data;
  }
  catch (error) {
    console.error("获取单个阶段的信息失败", error);
    throw new Error("获取单个阶段的信息失败");
  }
}

// 更新阶段介绍字段
export const updatePhaseIntroduction = async (sceneId: number, phaseId: number, information: string) => {
  try {
    const response = await api.post(`/java/ground/aix/phase/update_phase_instruction`, { sceneId, phaseId, information })
    return response.data
  }
  catch (error) {
    console.error("更新阶段介绍字段失败", error);
    throw new Error("更新阶段介绍字段失败");
  }
}

// 更新阶段目标字段
export const updatePhaseTarget = async (sceneId: number, phaseId: number, information: string) => {
  try {
    const response = await api.post(`/java/ground/aix/phase/update_phase_target`, { sceneId, phaseId, information })
    return response.data
  }
  catch (error) {
    console.error("更新阶段目标字段失败", error);
    throw new Error("更新阶段目标字段失败");
  }
}

// 更新阶段作业字段
export const updatePhaseHomework = async (sceneId: number, phaseId: number, information: string, score: number) => {
  try {
    const response = await api.post(`/java/ground/aix/phase/update_work`, { sceneId, phaseId, information, score })
    return response.data
  }
  catch (error) {
    console.error("更新阶段作业字段失败", error);
    throw new Error("更新阶段作业字段失败");
  }
}

// 删除阶段作业
export const deletePhaseHomework = async (sceneId: number, phaseId: number) => {
  try {
    // query参数
    const response = await api.post(`/java/ground/aix/phase/delete_work?sceneId=${sceneId}&phaseId=${phaseId}`)
    return response.data
  }
  catch (error) {
    console.error("删除阶段作业失败", error);
    throw new Error("删除阶段作业失败");
  }
}

// 添加阶段作业
export const addPhaseHomework = async (sceneId: number, phaseId: number) => {
  try {
    // query参数，放到路径里面
    const response = await api.post(`/java/ground/aix/phase/insert_work?sceneId=${sceneId}&phaseId=${phaseId}`)
    return response.data
  }
  catch (error) {
    console.error("添加阶段作业失败", error);
    throw new Error("添加阶段作业失败");
  }
}

// 阶段作业-获取已提交作业列表
export const getPhaseSubmittedHomeworkListApi = async (beginEndId: string, phaseId: string) => {
  try {
    const response = await api.get(`/java/ground/aix/opus/get_phase_opus`, {
      params: {
        beginEndId,
        phaseId,
      },
    })
    return response.data
  }
  catch (error) {
    console.error("获取已提交作业列表失败", error);
    throw new Error("获取已提交作业列表失败");
  }
}

// 阶段作业-删除提交作业
export const deletePhaseSubmittedHomeworkApi = async (beginEndId: string, phaseId: string) => {
  try {
    const response = await api.post(`/java/ground/aix/opus/delete_phase_opus/`, {}, {
      params: {
        beginEndId,
        phaseId,
      },
    })
    return response.data
  }
  catch (error) {
    console.error("删除提交作业失败", error);
    throw new Error("删除提交作业失败");
  }
}

// 阶段作业-添加提交作业
export const addPhaseSubmittedHomeworkApi = async (beginEndId: string, phaseId: string, introduction: string, tags: string[], opusTitle: string, coverImage: any, opusFile: any) => {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    formData.append('opusFile', opusFile);
    // 将tags转换为字符串，使用英文逗号进行分割
    const tagsString = tags.join(',');
    const response = await api.post(`/java/ground/aix/opus/submit_phase_opus`, formData, { params: { beginEndId, phaseId, introduction, tags: tagsString, opusTitle } })
    return response.data
  }
  catch (error) {
    console.error("添加提交作业失败", error);
    throw new Error("添加提交作业失败");
  }
}

// 获取微场景下的开课记录
export const getMicroSceneBeginEndIdApi = async (sceneId: string) => {
  try {
    const response = await api.get(`/java/ground/aix/scene/get_scene_begin_end_record`, {
      params: {
        sceneId,
      },
    })
    return response.data
  }
  catch (error) {
    console.error("获取微场景下的开机记录失败", error);
    throw new Error("获取微场景下的开机记录失败");
  }
}

// 获取微场景下子阶段提交作品
export const getSubPhaseSubmittedHomeworkApi = async (beginEndId: string, subPhaseId: string) => {
  try {
    const response = await api.get(`/java/ground/aix/opus/get_sub_phase_opus`, { params: { beginEndId, subPhaseId } })
    return response.data
  }
  catch (error) {
    console.error("获取微场景下子阶段提交作品失败", error);
    throw new Error("获取微场景下子阶段提交作品失败");
  }
}

// 添加子阶段提交作品
export const addSubPhaseSubmittedHomeworkApi = async (beginEndId: string, subPhaseId: string, introduction: string, tags: string[], opusTitle: string, coverImage: any, opusFile: any) => {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    formData.append('opusFile', opusFile);
    // 将tags转换为字符串，使用英文逗号进行分割
    const tagsString = tags.join(',');
    const response = await api.post(`/java/ground/aix/opus/submit_sub_phase_opus`, formData, { params: { beginEndId, subPhaseId, introduction, tags: tagsString, opusTitle } })
    return response.data
  }
  catch (error) {
    console.error("添加子阶段提交作品失败", error);
    throw new Error("添加子阶段提交作品失败");
  }
}

// 删除子阶段提交作品
export const deleteSubPhaseSubmittedHomeworkApi = async (beginEndId: string, subPhaseId: string) => {
  try {
    const response = await api.post(`/java/ground/aix/opus/delete_sub_phase_opus`, {}, { params: { beginEndId, subPhaseId } })
    return response.data
  }
  catch (error) {
    console.error("删除子阶段提交作品失败", error);
    throw new Error("删除子阶段提交作品失败");
  }
}
